package com.xugongzhang.view;

import com.xugongzhang.service.FileClientService;
import com.xugongzhang.service.MessageClientService;
import com.xugongzhang.service.UserClientService;

import java.util.Scanner;

/**
 * @author X
 * @date 2022/11/26 14:54
 */
public class QQView {
    // 控制是否显示菜单
    private Boolean loop = true;
    // 接收用户输入的指令
    private String key = "";
    
    private UserClientService userClientService = new UserClientService();
    private MessageClientService messageClientService = new MessageClientService();
    private FileClientService fileClientService = new FileClientService();

    public static void main(String[] args) {
        new QQView().mainMenu();
    }
    
    private void mainMenu() {
        while (loop) {
            System.out.println("=======欢迎登陆网络通讯=======");
            System.out.println("\t\t 1 登陆系统");
            System.out.println("\t\t 9 退出系统");

            Scanner scanner = new Scanner(System.in);
            System.out.print("请选择：");
            int i = scanner.nextInt();
            // 根据用户的输入处理不同的逻辑
            switch (String.valueOf(i)) {
                case "1":
                    System.out.print("username:");
                    String userId = scanner.next();
                    System.out.print("password:");
                    String password = scanner.next();
                    // 假设合法，用户登陆成功
                    if (userClientService.checkUser(userId, password)) {
                        System.out.println("=====登陆成功！请继续选择：");
                        System.out.println("=======网络通讯二级菜单=======");
                        System.out.println("\t\t 1 显示在线用户");
                        System.out.println("\t\t 2 群发消息");
                        System.out.println("\t\t 3 私聊消息");
                        System.out.println("\t\t 4 发送文件");
                        System.out.println("\t\t 9 退出系统");
                        System.out.print("请选择：");
                        while (loop) {
                            int i1 = scanner.nextInt();
                            switch (String.valueOf(i1)) {
                                case "1":
                                    // 获取在线用户
                                    userClientService.getOnLineUserList(userId);
                                    break;
                                case "2":
                                    System.out.print("请输入想对大家说的话：");
                                    String toOthers = scanner.next();
                                    messageClientService.sendMsgToAll(toOthers, userId);
                                    break;
                                case "3":
                                    System.out.print("信息要发给谁：");
                                    String receive = scanner.next();
                                    System.out.println("消息内容（100字）：");
                                    String info = scanner.next();
                                    messageClientService.sendMessageToOne(info, userId, receive);
                                    break;
                                case "4":
                                    System.out.print("请输入接收文件的用户：");
                                    String getter = scanner.next();
                                    System.out.print("请输入传输的文件路径：");
                                    String src = scanner.next();
                                    System.out.print("请输入对方的文件路径：");
                                    String destPath = scanner.next();
                                    fileClientService.sendFileToOne(src, destPath, userId, getter);
                                    break;
                                default:
                                    userClientService.logout();
                                    loop = false;
                            }
                        }
                    }
                    else {
                        System.out.println("登陆失败");
                    }
                    break;
                default:
                    System.out.println("退出系统");
                    loop = false;
            }
        }
    }
}
